package com.ctshk.rpc.order.train.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.common.dto.Result;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.order.train.dto.TrainOrderReceiptDTO;
import com.ctshk.rpc.order.train.entity.TrainManualOrder;
import com.ctshk.rpc.order.train.entity.TrainOrderReceipt;
import com.ctshk.rpc.order.train.mapper.TrainOrderReceiptMapper;
import com.ctshk.rpc.order.train.req.TrainOrderReceiptReq;
import com.ctshk.rpc.order.train.service.ITrainOrderReceiptService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * <p>
 * 火车订单收据 服务实现类
 * </p>
 *
 * @author 彭柳霖
 * @since 2021-03-18
 */
@DubboService
public class TrainOrderReceiptServiceImpl extends ServiceImpl<TrainOrderReceiptMapper, TrainOrderReceipt> implements ITrainOrderReceiptService {

    @Autowired
    private TrainOrderReceiptMapper trainOrderReceiptMapper;

    /**
     * 保存订单收据(新增或修改)
     * @param trainOrderReceiptReq
     * @param user
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result saveTrainReceipt(TrainOrderReceiptReq trainOrderReceiptReq, TokenUser user) {

        TrainOrderReceipt trainOrderReceipt = trainOrderReceiptMapper.selectOne(new QueryWrapper<TrainOrderReceipt>().lambda().eq(TrainOrderReceipt::getOrderId, trainOrderReceiptReq.getOrderId()));

        if(Objects.isNull(trainOrderReceipt)){
            //新增
            trainOrderReceipt.setId(SnowflakeIdWorker.nextId());
            trainOrderReceipt= EntityUtil.copy(trainOrderReceiptReq, TrainOrderReceipt.class);
            trainOrderReceipt.setGmtCreate(LocalDateTime.now());
            trainOrderReceipt.setCreateId(user.getId());
            trainOrderReceiptMapper.insert(trainOrderReceipt);
        }else{
            //修改
            trainOrderReceipt= EntityUtil.copy(trainOrderReceiptReq, TrainOrderReceipt.class);
            trainOrderReceipt.setModifiedId(user.getId());
            trainOrderReceipt.setGmtModified(LocalDateTime.now());
            trainOrderReceiptMapper.updateById(trainOrderReceipt);
        }

        return Result.success();
    }


    /**
     * 根据订单id查询订单收据详细信息
     *
     * @param orderId 订单id
     * @return 订单消息
     */
    @Override
    public Result<TrainOrderReceiptDTO> queryOrderReceiptInfo(Long orderId) {
        TrainOrderReceipt trainOrderReceipt = trainOrderReceiptMapper.selectOne(new QueryWrapper<TrainOrderReceipt>().lambda().eq(TrainOrderReceipt::getOrderId,orderId));
        TrainOrderReceiptDTO trainOrderReceiptDTO = EntityUtil.copy(trainOrderReceipt, TrainOrderReceiptDTO.class);
        return Result.success(trainOrderReceiptDTO);
    }
}
